package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    //套餐管理
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.根据那么模糊分页查询套餐
        // 1.构建菜品条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);

        // 2.构建分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        // 3.查询分页
        page = setmealMapper.selectPage(page, qw);

        List<Setmeal> setmeals = page.getRecords();
        for (Setmeal setmeal : setmeals) {
            //2.根据setmeal的category_id查询category
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());

            //3.根据setmeal的setmeal_dish查询列表
            //构建条件
            LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
            dqw.eq(SetmealDish::getDishId, setmeal.getId());
            // 查询列表
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(dqw);
            setmeal.setSetmealDishes(setmealDishList);
        }
        //4.组装返回结果
        return page;
    }


    //回显菜品列表
    @Override
    public List<Dish> findById(Long id,String name) {
        // 构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();

        //如果name是null就执行查询菜品
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        //如果categoryid是null就执行模糊查询
        qw.eq(id != null,Dish::getCategoryId, id);

        // 调用mapper查询，并返回
        return dishMapper.selectList(qw);
    }

    //新增套餐
    // 清理整个缓存区域
    @CacheEvict(value = "setmeal",allEntries = true)
    @Override
    public void saveSetmeal(Setmeal setmeal) {
        //存储套餐基本信息
        setmealMapper.insert(setmeal);

        //遍历套餐的菜品添加到数据库
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());

            //添加到数据库
            setmealDishMapper.insert(setmealDish);
        }
    }

    // 清理整个缓存区域
    @CacheEvict(value = "setmeal",allEntries = true)
    //删除套餐
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //构建条件查询ids中是否有正在启售中的套餐
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<Setmeal>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);

        //判断是否存在启售中的套餐
        Integer count = setmealMapper.selectCount(qw);
        if (count > 0){
            throw new CustomException("删除失败..有套餐正在启售中...");
        }
        //删除
        setmealMapper.deleteBatchIds(ids);

        //删除套餐菜品
        LambdaQueryWrapper<SetmealDish> sqw = new LambdaQueryWrapper<>();
        sqw.in(SetmealDish::getSetmealId,ids);

        //调用mapper删除
        setmealDishMapper.delete(sqw);
    }


    //修改时回显数据
    @Override
    public Setmeal findByIdd(Long id) {
        //调用mapper查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);

        //根据id查询套餐下的菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);

        //存入套餐
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    //修改套餐
    // 清理整个缓存区域
    @CacheEvict(value = "setmeal",allEntries = true)
    @Override
    public void updateSetmeal(Setmeal setmeal) {
        //修改基本信息
        setmealMapper.updateById(setmeal);

        //删除旧菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());

        setmealDishMapper.delete(qw);

        //添加新菜品
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //关联id
            setmealDish.setSetmealId(setmeal.getId());

            //添加到数据库
            setmealDishMapper.insert(setmealDish);
        }
    }

    //起售,停售
    // 清理整个缓存区域
    @CacheEvict(value = "setmeal",allEntries = true)
    @Override
    public void updateStatusSetmeal(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);

        //构建实体类
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);

        //修改状态
        setmealMapper.update(setmeal,qw);
    }

    // 根据分类id+状态查询套餐列表
    @Cacheable(value = "setmeal",key = "#categoryId")
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        // 1.构建条件   select * from setmeal where category_id = ? and status = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);
        qw.eq(Setmeal::getStatus, status);
        // 2.查询列表
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        // 3.返回结果
        return setmealList;
    }

    // 查看套餐详情
    @Override
    public List<Dish> findDishDetail(Long id) {
        // 1.查询套餐菜品中间表集合
        // 1-1 构建条件  select * from setmeal_dish where setmeal_id = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        // 1-2 查询
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        // 2.遍历
        List<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            // 2-1 查询菜品
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            // 2-2 添加套餐中数量
            dish.setCopies(setmealDish.getCopies());
            // 2-3 添加返回菜品集合
            dishList.add(dish);
        }
        // 3.返回结果
        return dishList;
    }
}
